package com.nowcoder.controller;

import com.nowcoder.entity.Event;
import com.nowcoder.entity.Page;
import com.nowcoder.entity.User;
import com.nowcoder.event.EventProducer;
import com.nowcoder.service.FollowService;
import com.nowcoder.service.UserService;
import com.nowcoder.utils.CommunityConstant;
import com.nowcoder.utils.CommunityUtil;
import com.nowcoder.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {
    @Autowired
    FollowService followService;

    @Autowired
    EventProducer eventProducer;

    @Autowired
    UserService userService;

    @PostMapping("/follow")
    @ResponseBody
    public String follow(int entityType, int entityId) {
        User user = HostHolder.getUser();
        followService.follow(user.getId(), entityType, entityId);

        // 触发关注事件
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(HostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserid(entityId);
        eventProducer.fireEvent(event);


        return CommunityUtil.getJSONString(0, "已关注");
    }

    @PostMapping("/unfollow")
    @ResponseBody
    public String unfollow(int entityType, int entityId) {
        User user = HostHolder.getUser();
        followService.unFollow(user.getId(), entityType, entityId);
        return CommunityUtil.getJSONString(0, "已取消关注");
    }

    // 查询全部关注
    @GetMapping("/followees/{userId}")
    public String getFolloweeList(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.findUserById(userId);
        if(user == null) {
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user", user);
        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int) followService.findFolloweeCount(userId, CommunityConstant.ENTITY_TYPE_USER));
        List<Map<String, Object>> followeeList = followService.findFollowees(userId, page.getOffset(), page.getLimit());
        for(Map<String, Object> map: followeeList) {
            User u = (User) map.get("user");
            map.put("hasFollowed", hasFollowed(u.getId()));
        }
        model.addAttribute("users", followeeList);
        return "/site/followee";
    }


    // 查询全部粉丝
    @GetMapping("/followers/{userId}")
    public String getFollowerList(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.findUserById(userId);
        if(user == null) {
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user", user);
        page.setLimit(5);
        page.setPath("/followers/" + userId);
        page.setRows((int) followService.findFollowerCount(userId, CommunityConstant.ENTITY_TYPE_USER));
        List<Map<String, Object>> followerList = followService.findFollowers(userId, page.getOffset(), page.getLimit());
        for(Map<String, Object> map: followerList) {
            User u = (User) map.get("user");
            map.put("hasFollowed", hasFollowed(u.getId()));
        }
        model.addAttribute("users", followerList);
        return "/site/follower";
    }

    private boolean hasFollowed(int userId) {
        if(HostHolder.getUser() == null) {
            return false;
        }
        return followService.hasFollowed(HostHolder.getUser().getId(), CommunityConstant.ENTITY_TYPE_USER, userId);
    }
}










